package com.zyark.web.filter;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * ClassName: EmojiFilterUtils  微信昵称表情过滤<br/>
 * Function:  <br/>
 * Reason:  <br/>
 * Date: 2017-06-15 21:44 <br/>
 *
 * @author Ku_ker
 * @version 1.0
 * @JDK 1.7
 */
public class EmojiFilter {

    public static String filterEmoji(String source) {
        if (source != null) {

//            Pattern emoji = Pattern.compile("\\\\x[\\\\x80-\\\\xFF][\\\\x80-\\\\xFF]");
            Pattern emoji = Pattern.compile("\\\\xEE[\\\\x80-\\\\xBF][\\\\x80-\\\\xBF]|\\\\xEF[\\\\x81-\\\\x83" +
                    "][\\\\x80-\\\\xBF]");
            Matcher emojiMatcher = emoji.matcher(source);
            if (emojiMatcher.find()) {
                source = emojiMatcher.replaceAll("");
            }

            Pattern emoji1 = Pattern.compile("\\\\xE0[\\\\x80-\\\\xBF][\\\\x80-\\\\xBF]|\\\\xEF[\\\\x81-\\\\x83" +
                    "][\\\\x80-\\\\xBF]");
            Matcher emojiMatcher1 = emoji1.matcher(source);
            if (emojiMatcher1.find()) {
                source = emojiMatcher1.replaceAll("?");
            }
            return source;
        }
        return source;
    }


    public static String filter(String source) {

        String str_Result = "", str_OneStr = "";
        for (int z = 0; z < source.length(); z++) {
            str_OneStr = source.substring(z, z + 1);
            if (str_OneStr.matches("[\u4e00-\u9fa5]+") || str_OneStr.matches("[\\x00-\\x7F]+")) {
                str_Result = str_Result + str_OneStr;
            }
        }
        return str_Result;

    }
}